#include "ApplicationH.h"


void Application::init()
{
	generalMenu.Init(9);
	rectangleMenu.Init(5);
	
	generalMenu.set(0, "Create Rectangle");
	generalMenu.set(1, "Draw with cutovers");
	generalMenu.set(2, "Change default drawing option");
	generalMenu.set(3, "Choose Rectangle");
	generalMenu.set(8, "Exit");


	rectangleMenu.set(0, "Cancel Selection");
	rectangleMenu.set(1, "Erase");
	rectangleMenu.set(2, "Bring To Front Screen");
	rectangleMenu.set(3, "Bring To Foreground");
	rectangleMenu.set(4, "Move Rectangle");

	choiceG = choiceR = 0;
	defaultPainting = 1;
}



void Application::applicationRun()
{
	  do
	  {
		choiceG = generalMenu.choose();

		switch(choiceG){

        case 1: 
        AppGeneralFirstChoice(); //Create Rectangle
        break;

		case 2: 
        AppGeneralSecondChoice(); //Draw with cutovers
        break;

		case 3: 
        AppGeneralThirdChoice(); //Change default drawing option
        break;
		
		case 4: 
        AddGeneralFourthChoice(); //Choose Rectangle
        break;

		case 9: 
        cout << endl << endl << "Bye Bye thank you :D" << endl; //Exit
			break;

		default: {
			cout << "Not valid, please choose correct option from menu";  
			_getch();
				 }

		}

	  }while(choiceG!=9); //Exit (VIA MAIN)

}


//Create Rectangle
void Application::AppGeneralFirstChoice()
{
	Rectangl * newRectangle = new Rectangl;
	//ListRectangle.AddRecToTail(newRectangle); // Old design
	RectangleL.addNewRectangeToHead(newRectangle);
}

//Draw with cutovers
void  Application::AppGeneralSecondChoice()
{
	system("cls");
	//ListRectangle.DrawList(defaultPainting); // Old design
	RectangleL.DrawList(defaultPainting);
	_getch();
}


//Change default drawing option
void Application::AppGeneralThirdChoice()
{
	if (defaultPainting == 1)
	{
		generalMenu.set(1, "Draw without cutovers");
		defaultPainting--;
	}

	else{
		generalMenu.set(1, "Draw with cutovers");
		defaultPainting++;
	}
}



void Application::AddGeneralFourthChoice()
{
	int x, y;
	Rectangl * temp;

	system("cls");
	
	cout << "Please enter x coordination " << endl;
	cin >> x;

	cout << "Please enter y coordination " << endl;
	cin >> y;
    
	if(RectangleL.isPointExistInList(x,y,&temp))
    {
		goMenuRectangle(temp,x,y);
	}

	else
	{
	   system("cls");
	   cout << "The input point doesn't exist" << endl;
	   cout << "Press any key to go back to the menu" << endl;
	   _getch();
	}
}


void Application::goMenuRectangle(Rectangl * r, int x, int y)
{
	system("cls");
	RectangleL.DrawList(defaultPainting);
	r->setSymbolRectangle('+');
	r->goAndDrawRectangle(1);
	
	goAndDrawPointFromUser(x,y,'@');
	
	_getch();

	r->setSymbolRectangle('*');

	do
	  {
		choiceR = rectangleMenu.choose();

		switch(choiceR){

        case 1: //Cancel Selection
        break;

		case 2: 
        AppRectangleSecondChoice(r); //Erase
        break;

		case 3: 
        AppRectangleThirdChoice(r); //Bring To Front Screen
        break;
		
		case 4: 
        AppRectangleFourthChoice(r); //Bring To Foreground
        break;

		case 5: 
        AppRectangleFifthChoice(r); //Move Rectangle
        break;

		default: {
			cout << "Not valid, please choose correct option from menu";  
			_getch();
				 }

		}

	  }while(choiceR!=1);


}

void Application::goAndDrawPointFromUser(int x, int y, char c)
{
	if(x < 0 || x >79)
		if(y < 0 || y > 24)
			return;
	
	gotoxy(x,y);
	cout << c;
}

//Erase
void Application::AppRectangleSecondChoice(Rectangl * r)
{
	if(RectangleL.removeFromList(r)) {
		system("cls");
		cout << "The selected rectangle has been erased" << endl;
		cout << "Press any key to continue ..";
		_getch();
	}
	else cout << "The selected rectangle was not erased"; // not relevant in our application anyway
	
	choiceR = 1;
}

//Bring To Front Screen
void Application::AppRectangleThirdChoice(Rectangl * r)
{
	RectangleL.BringToFront(r);
	system("cls");
	cout << "ok !" << endl << endl;
	cout << "Press any key to continue ..";
	_getch();
}

//Bring To Foreground
void Application::AppRectangleFourthChoice(Rectangl * r)
{
	RectangleL.BringToBack(r);
	system("cls");
	cout << "ok !" << endl << endl;
	cout << "Press any key to continue ..";
	_getch();
}

//Move Rectangle
void Application::AppRectangleFifthChoice(Rectangl *r)
{

	int dx,dy,x,y;
	
	cout << endl;
	cout << "Move rectangle X by : ";
	cin >> dx;
	cout << "Move rectangle Y by : ";
	cin >> dy;

	//Now reposition
	r->getPos(x,y);
	r->resetPos(x+dx,y+dy);
	//Finished reposition 

	system("cls");
	cout << "ok !" << endl << endl;
	cout << "Press any key to continue ..";

	_getch();

}